//
//  VistorLoginView.swift
//  weibo-mxm
//
//  Created by 马晓敏 on 16/1/11.
//  Copyright © 2016年 马晓敏. All rights reserved.
//

import UIKit
//导入SNAPkit命名空间
import SnapKit

//1.swift中协议的特点 
/*
>protocol 前没有@
>遵守根协议 <NSObject>
>如果不遵守基协议,就无法使用weak
>声明可选的协议的方法,需要使用optional
>需要在声明的协议前加上@objc

*/

@objc protocol VistorLoginViewDelegate:NSObjectProtocol{
//协议方法
    //用户将要注册
    optional func userWillRegister()
    //用户将要登录
    func userWillLogin()

}
class VistorLoginView: UIView {
//声明代理 swift属性,默认的属性关键字 strong
    //声明弱引用的属性,需要weak
    
    weak var vistorViewDelegate:VistorLoginViewDelegate?
    
 //MARK:按钮的监听事件方法
    @objc private func loginBtnDidClick (){
    
    //使用代理调用此方法
      vistorViewDelegate?.userWillLogin()
    
    
    }
    
    @objc  private func registerBtnDidClick(){
    
    vistorViewDelegate?.userWillRegister?()
    
    }
    
    
    
    
    
    
    //外界调用的重要方法
    func setupUIWithInfo(imageName:String?,tipText:String){
        if let img = imageName{
            circleIcon.image = UIImage(named: img)
//隐藏大房子
            largeHouse.hidden = true
         bringSubviewToFront(circleIcon)
        }else{
       
            startAnimation()
        
        }
               tipLabel.text = tipText
        
    }
  
    func startAnimation(){
    //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
       base.repeatCount = MAXFLOAT
       base.toValue = 2 * M_PI
      base.duration = 10;
        //添加动画
        //设置动画完成//页面失去活跃状态 不移除图层
        base.removedOnCompletion = false
       circleIcon.layer.addAnimation(base, forKey: nil)
    
    
    }
    
    
    
    override init(frame: CGRect) {
     super.init(frame: frame)
    setupUI()
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
    private func setupUI(){
    //添加子视图
    addSubview(circleIcon)
          addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
      
     //设置自动布局 手码布局 通过VFL 
    
  /*item 表示要设置约束的对象
      attribute 需要添加约束的属性
        relatedBy 参照方法 一般使用精准参照
       toItem 被参照的对象
*/
        
//将约束对象添加到父视图上
        //注意一点 自动布局能够生效 需要屏蔽frame布局方式
//    circleIcon.translatesAutoresizingMaskIntoConstraints = false
//     self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
   //添加约束推荐使用链式约束
        
    circleIcon.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
    largeHouse.snp_makeConstraints { (make) -> Void in
    make.center.equalTo(circleIcon.snp_center)
        }
     
tipLabel.snp_makeConstraints { (make) -> Void in
    make.centerX.equalTo(circleIcon)
    make.top.equalTo(circleIcon.snp_bottom).offset(16)
    make.width.equalTo(225)
    make.height.equalTo(40)
    
        }
        
  loginBtn.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(tipLabel.snp_left)
    make.top.equalTo(tipLabel.snp_bottom)
    make.width.equalTo(100)
    
        }
    registerBtn.snp_makeConstraints { (make) -> Void in
        make.right.equalTo(tipLabel.snp_right)
        make.top.equalTo(tipLabel.snp_bottom)
    make.width.equalTo(100)
        }
        
       backView.snp_makeConstraints { (make) -> Void in
//        make.top.equalTo(self.snp_top)
//        make.left.equalTo(self.snp_left)
//        make.right.equalTo(self.snp_right)
        make.top.left.right.equalTo(self)
        make.bottom.equalTo(registerBtn.snp_bottom)
        }
     self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
     //添加监听事件 
    loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    
   //背景
    lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
  //懒加载获取所有的子视图
    lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon") )
     lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house") )
    
    //提示文案
    lazy var tipLabel :UILabel = {
    let l = UILabel()
     l.font = UIFont.systemFontOfSize(14)
        l.text = "登录后,别人评论你得微博,发给你得消息,都会在这里受到通知"
    l.textColor = UIColor.darkGrayColor()
    l.textAlignment = .Center
    l.numberOfLines = 0
        
        l.sizeToFit()
        return l
    
    }()
 //登录按钮
    lazy var loginBtn:UIButton = {
     let btn = UIButton()
    
   btn.setTitle("登录", forState: .Normal)
   btn.titleLabel?.font = UIFont.systemFontOfSize(18)
   btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
   let img = UIImage(named: "common_button_white_disable")
   let w = Int(img!.size.width*0.5)
   let h = Int(img!.size.height*0.5)
        btn.setBackgroundImage(img?.stretchableImageWithLeftCapWidth(w, topCapHeight: h),forState:.Normal)
    
    return btn
    }()
    
    lazy var registerBtn:UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
         btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
        }()
    
  
    
    
    
    
   }
